<template>
	<view class="pages">
		<u-navbar title="仓库" :autoBack="true" placeholder bgColor="#fff"></u-navbar>
		<view class="top-search">
      <u-search
        placeholder="搜索仓库名称"
        @search="search"
        :showAction="false"
      ></u-search>
		</view>
    <u-line></u-line>
    <view>
      <u-cell v-for="(item, index) in list" :key="index" :name="item.join(',')" :title="item[1]" :value="item[2] || ''" isLink @click="selectOrg"></u-cell>
    </view>
	</view>
</template>

<script>
import { getMaterialInformation, dynamicFormServiceSave, synchronousWarehouse } from '@/api/index'

export default {
  data() {
    return {
      dataList: [],
      list: [],
      orgInfo: {},
      type: '',
    }
  },
  onLoad(options) {
    this.type = options.type
    this.orgInfo = uni.getStorageSync('orgInfo')
    this.getPage()
  },
  methods: {
    getPage() {
      uni.showLoading({ mask: true });
      const parameters = [
        {
          "FormId":"BD_STOCK",
          "FieldKeys":"fstockid,fname,fnumber,FUseOrgId.fnumber",
          "FilterString": "FDocumentStatus='C' and FISOPENLOCATION='1' and FForbidStatus='A' and FUseOrgId.fnumber="+this.orgInfo.orgId,
          "OrderString":"fnumber ASC",
          "TopRowCount":0,
          "Limit":2000,
          "StartRow":0,
        }
      ]
      getMaterialInformation({parameters}).then(res => {
        uni.hideLoading();
        this.dataList = res
        this.list = res
      })
    },
    search(e) {
      if(e) {
        this.list = this.dataList.filter(v => v[1].includes(e))
      } else {
        this.list = [...this.dataList]
      }
    },
    selectOrg(e) {
      const info = e.name.split(',')
      if(this.type) {
        const that = this
        uni.showModal({
          title: '提示',
          editable: true,
          placeholderText: '请输入仓位编码',
          success: async function (res) {
            if (res.confirm) {
              if(res.content == '') {
                that.selectOrg(e)
              } else {
                uni.showLoading({ mask: true });
                let parameters = [
                  "BD_FLEXVALUES",
                  {
                    "IsDeleteEntry":"false",
                    "Model":{
                      "FID": info[2] == 'CK012'?'100002':'100003',
                      "FNUMBER": info[2] == 'CK012'?'002':'003',
                      "FNAME": info[2] == 'CK012'?'长春总仓货位':'长春总仓备货区货位',
                      "FFlexTypeId": "StockPlace",
                      "FEntity":[
                        {
                          "FEntryID": 0,
                          "FFlexValueNumber": res.content,
                          "FFlexValueName": res.content
                        }
                      ]
                  }
                  }
                ]
                const res1 = await dynamicFormServiceSave({parameters})
                if(res1.Result.ResponseStatus.IsSuccess) {
                  await synchronousWarehouse({ stocknumber: JSON.stringify({stocknumber: info[2]}) })
                  uni.hideLoading()
                  uni.showToast({
                    title: '新增仓位成功',
                    icon: "success",
                  });
                  setTimeout(function() {
                    that.goBack()
                  }, 1500)
                } else {
                  uni.hideLoading()
                  uni.showToast({
                    title: res1.Result.ResponseStatus.Errors[0].Message,
                    icon: "none",
                  });
                }
              }
            }
          }
        });
      } else {
        let pages = getCurrentPages()
        let prevPage = pages[pages.length - 2]
        prevPage.$vm.form.FSrcStockName = info[1]
        prevPage.$vm.form.FSrcStockId = info[2]
        this.goBack()
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.pages{
  width: 750rpx;
  min-height: 100vh;
  padding-bottom: 40rpx;
  .top-search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 30rpx 20rpx;
    margin-top: 20rpx;
  }
}
</style>